package br.jus.tjpe.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import br.jus.tjpe.dominio.Vara;
import br.jus.tjpe.negocio.VaraServico;
import br.jus.tjpe.util.MessageResource;
import br.jus.tjpe.util.MessageResourceFactory;

public class VaraBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<Vara> varas;

	private Vara vara;

	private Vara vara1;

	private Vara vara2;

	private Vara vara3;

	private Vara vara4;

	private Vara vara5;

	@EJB
	private VaraServico varaServico;

	public List<Vara> getVaras() {
		return varas;
	}

	public void setVaras(List<Vara> varas) {
		this.varas = varas;
	}

	public Vara getVara() {
		return vara;
	}

	public void setVara(Vara vara) {
		this.vara = vara;
	}

	public Vara getVara1() {
		return vara1;
	}

	public void setVara1(Vara vara1) {
		this.vara1 = vara1;
	}

	public Vara getVara2() {
		return vara2;
	}

	public void setVara2(Vara vara2) {
		this.vara2 = vara2;
	}

	public Vara getVara3() {
		return vara3;
	}

	public void setVara3(Vara vara3) {
		this.vara3 = vara3;
	}

	public Vara getVara4() {
		return vara4;
	}

	public void setVara4(Vara vara4) {
		this.vara4 = vara4;
	}

	public Vara getVara5() {
		return vara5;
	}

	public void setVara5(Vara vara5) {
		this.vara5 = vara5;
	}

	public String manterVara() {
		this.varas = this.varaServico.consultarVaras();

		return "manterVara";
	}

	public String cadastrarVara() {
		this.vara = new Vara();

		return "cadastrarVara";
	}

	public String cadastrarVaras() {
		this.vara1 = new Vara();
		this.vara2 = new Vara();
		this.vara3 = new Vara();
		this.vara4 = new Vara();
		this.vara5 = new Vara();

		return "cadastrarVaras";
	}

	public String removerVara() {
		this.varaServico.remover(this.vara);
		this.varas = this.varaServico.consultarVaras();
		return "manterVara";
	}

	public String editarVara() {
		return "cadastrarVara";
	}

	public String salvar() {
		String outcome = null;
		boolean valido = true;

		FacesContext context = FacesContext.getCurrentInstance();
		MessageResource message = MessageResourceFactory.createMessageResource("mensagens");

		if (vara.getNome() == null || vara.getNome().isEmpty()) {
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, message.get("vara.nome.obrigatorio"),
					message.get("vara.nome.obrigatorio")));
			valido = false;
		}

		if (valido) {
			this.varaServico.salvar(vara);
			this.varas = this.varaServico.consultarVaras();
			outcome = "manterVara";
		} else {
			outcome = "cadastrarVara";
		}

		return outcome;
	}
	
	public String salvarVaras(){
		String outcome = null;
		List<Vara> varas = new ArrayList<Vara>();
		varas.add(vara1);
		varas.add(vara2);
		varas.add(vara3);
		varas.add(vara4);
		varas.add(vara5);
		
		FacesContext context = FacesContext.getCurrentInstance();
		MessageResource message = MessageResourceFactory.createMessageResource("mensagens");

		try {
			this.varaServico.salvar(varas);
			this.varas = this.varaServico.consultarVaras();
			outcome = "manterVara";
		} catch (Exception e) {
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, message.get("vara.nome.curto.obrigatorio"),
					message.get("vara.nome.curto.obrigatorio")));

			outcome = "cadastrarVaras";
		}

		return outcome; 
	}

}
